<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 订单
 */

namespace app\api\controller\user\v1_0\bus;


use think\Db;
class Order extends \app\api\controller\user\v1_0\Common
{
    protected $noNeedRight = ['test'];
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 确认订单
     */    
    public function confirm(){
        $id = request()->param('id');
        $order = (new \app\common\model\BusOrder())->where([
            'id' => $id
        ])->find();
  
        // if($order['bus_state'] == 2){
        //     return self::errorResult("该笔订单已经处理完成！！！");
        // }
        Db::startTrans();
        try{
            $message = "订单创建失败,请重试";
            $process = true;
            $order = (new \app\common\model\UserOrders())->where([
                'bus_order_id' => $order['id'],'state' => 1
            ])->select();
            foreach ($order as $key => $value) {
                $row = (new \app\common\model\UserOrders())->where(['id' => $value['id'],'state' => 1])->update([
                    'state' => 4,
                    'bus_state' => 2,
                ]);
                if(!$row){
                    $message = "修改订单状态失败,请重试";
                    $process = false;
                }
                #-------------------------------修改提货点资金日志-----------------------
                $bus = (new \app\common\model\Bus())->where(['id' => $value['bus_id']])->find();
                if($value['bus_smoney'] != 0){
                    $row = (new \app\common\model\Bus())->where([
                        'id' => $value['bus_id'],
                        'money' => $bus['money']
                    ])->update([
                        'money' => $bus['money']+$value['bus_smoney']
                    ]);
                    if(!$row){
                        $message = "修改资金失败,请重试";
                        $process = false;
                    }
                }
                $row = (new \app\common\model\BusMoneyLog())->insert([
                    'title' => "订单{$value['on']}收益",
                    'bus_id' => $value['bus_id'],
                    'type' => 0,
                    'money' => $value['bus_smoney'],
                    'addtime' => time(),
                    'order_id' => $id,
                    'agent_id' => $value['agent_id'],
                 ]); 
                if(!$row){
                    $message = "修改资金日志失败,请重试";
                    $process = false;
                }
                #------------------------------------修改用户资金日志-------------------------------
                $user = (new \app\common\model\User())->where(['id' => $value['share_user_id']])->find();
              
                if($value['user_smoney'] != 0){
                    $row = (new \app\common\model\User())->where([
                        'id' => $value['share_user_id'],
                        'money' => $user['money']
                    ])->update([
                        'money' => $user['money']+$value['user_smoney']
                    ]);
                    if(!$row){
                        $message = "修改资金失败,请重试";
                        $process = false;
                    }
                }
                $row = (new \app\common\model\UserMoneyLog())->insert([
                    'title' => "订单{$value['on']}收益",
                    'user_id' => $value['share_user_id'],
                    'type' => 1,
                    'money' => $value['user_smoney'],
                    'addtime' => time(),
                    'order_id' => $id,
                    'agent_id' => $value['agent_id'],
                 ]); 
                if(!$row){
                    $message = "修改资金日志失败,请重试";
                    $process = false;
                }
                $row = (new \app\common\model\BusOrder())->where(['id' => $id])->update([
                    'state' => 2,
                ]);
                if(!$row){
                    $message = "修改订单状态失败1,请重试";
                    $process = false;
                }
            }
            
           
            if ($process !== false) {
                Db::commit(); // 提交事务
            } else {
                Db::rollback(); // 回滚事务
            }
         } catch (\Exception $e) {
            $process = false;
            $message = $e->getMessage();
            Db::rollback(); // 回滚事务
        }
        if ($process !== false) {
            return self::successResult("成功");
        } else {
            return self::errorResult($message);
        }

    }

    
    

}
